package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tsd.core.shiro.helper.ShiroAuthorizationInfoHelper;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysRole;
import com.tsd.system.entity.SysRoleExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysRoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysRole 控制层
 *
 * @author Hillpool
 * @date 2019/03/12 12:20:51
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysRoleController extends SystemBaseController {

    private final SysRoleService sysRoleService;

    @Autowired
    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @ApiOperation(value = "保存SysRole")
    @RequiresPermissions("system:role:save")
    @ResponseBody
    @RequestMapping(value = "/saveSysRole.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysRole() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser sysUser = this.getSysUser();
            SysRoleExt t = super.getParams(request, SysRoleExt.class);
            Map<String, Object> map = super.getParams(request);
            Object userSidList = map.get("user_sid_list");
            sysRoleService.saveSysRole(t, sysUser);
            if (userSidList instanceof List) {
                List<String> user_sid_list = JSONArray.parseArray(JSON.toJSONString(userSidList), String.class);
                if (!HlpUtils.isEmptyList(user_sid_list)) {
                    sysRoleService.saveSysUserRoleList(t, sysUser, user_sid_list);
                }
            }
            ShiroAuthorizationInfoHelper.clearCache();
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据ids批量删除", notes = "根据ids批量删除")
    @RequiresPermissions("system:role:delete")
    @ResponseBody
    @RequestMapping(value = "/batchDelSysRole.action", method = RequestMethod.POST)
    public HttpResult<String> batchDelSysRole() throws HlpException {
        HttpResult<String> hr = new HttpResult<String>();
        SysUser opUser = super.getSysUser();
        try {
            Map<String, Object> map = super.getParams(request);
            Object obj = map.get("ids");
            super.checkEmpty(obj, "请提供参数ids");
            if (obj instanceof List) {
                List<Long> ids = JSONArray.parseArray(JSON.toJSONString(obj), Long.class);
                if (ids.size() == 0) {
                    throw new HlpException("请至少选择一项");
                }
                sysRoleService.batchDelSysRole(ids, opUser);
                hr.setCode(ErrorCode.CODE_SUCCESS);
            } else {
                throw new HlpException("数据类型不正确，非数组类型");
            }
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询系统角色")
    @ResponseBody
    @RequestMapping(value = "/findSysRoles.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysRoles() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "name", "name", false, true);
            HlpUtils.copyStringValue(map, params, "login_id", "login_id", false, true);
            HlpUtils.copyStringValue(map, params, "username", "username", false, true);
            HlpUtils.copyStringValue(map, params, "group_name", "group_name", false, true);
            HlpUtils.copyStringValue(map, params, "with_resource", "with_resource", false, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<SysRoleExt> list;
            if (map.containsKey("nopage")) {
                list = sysRoleService.findSysRoles(params);
            } else {
                list = sysRoleService.findSysRoles(params, page);
            }
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查找系统角色")
    @ResponseBody
    @RequestMapping(value = "/listRoles.action", method = RequestMethod.POST)
    public HttpResult<List<SysRoleExt>> listRoles() throws Exception {
        HttpResult<List<SysRoleExt>> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            String searchValue = (String) map.get("searchValue");
            if (!HlpUtils.isEmpty(searchValue)) {
                params.put("searchValue", "%" + searchValue + "%");
            }
            PageBean page = new PageBean(map);
            List<SysRoleExt> list = sysRoleService.listSysRole(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取SysRole树")
    @ResponseBody
    @RequestMapping(value = "/findSysRoleTree.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysRoleTree() {
        HttpResult<Object> hr = new HttpResult<Object>();
        try {
            Map<String, Object> map = super.getParams(request);
            String orgTypeSid = HlpUtils.getString(map, "orgTypeSid");
            List<SysRoleExt> list = sysRoleService.findSysRoleTree(orgTypeSid);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据单位类型获取单位类型下的角色")
    @ResponseBody
    @RequestMapping(value = "/findRoleByUnitType.action", method = RequestMethod.POST)
    public HttpResult<Object> findRoleByUnitType() {
        HttpResult<Object> hr = new HttpResult<Object>();
        try {
            Map<String, Object> map = super.getParams(request);
            String orgTypeSid = HlpUtils.getString(map, "orgTypeSid");
            List<SysRoleExt> list = null;
            if (orgTypeSid != null) {
                list = sysRoleService.findRoleByUnitType(orgTypeSid);
            }
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询角色列表")
    @ResponseBody
    @RequestMapping(value = "/findSysRoleList.action", method = RequestMethod.POST)
    public HttpResult<List<Map<String, Object>>> findSysRoleList() {
        HttpResult<List<Map<String, Object>>> hr = new HttpResult<List<Map<String, Object>>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            String orgTypeSid = HlpUtils.getString(map, "org_type_sid");
            Integer state = HlpUtils.getInteger(map, "state");
            Integer dr = HlpUtils.getInteger(map, "dr");
            String searchValue = HlpUtils.getString(map, "searchValue");
            if (!HlpUtils.isEmpty(orgTypeSid)) {
                params.put("orgTypeSid", orgTypeSid);
            }
            if (!HlpUtils.isEmpty(searchValue)) {
                params.put("searchValue", "%" + searchValue + "%");
            }
            if (!HlpUtils.isEmpty(state)) {
                params.put("state", state);
            }
            if (!HlpUtils.isEmpty(dr) && dr != -1) {
                params.put("dr", dr);
            }
            PageBean page = new PageBean(map);
            List<Map<String, Object>> list = sysRoleService.findParamsMapByPage(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "查询某个用户的角色列表")
    @ResponseBody
    @RequestMapping(value = "/findRoleListByUserSid.action", method = RequestMethod.POST)
    public HttpResult<List<SysRoleExt>> findRoleListByUserSid() {
        HttpResult<List<SysRoleExt>> hr = new HttpResult<List<SysRoleExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyStringValue(map, params, "userSid", "userSid", true, false);
            HlpUtils.copyStringValue(map, params, "org_type_sid", "org_type_sid", false, false);
            List<SysRoleExt> list = sysRoleService.findRoleListByUserId(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
